package cn.wolfcode.crm.web.controller;

import cn.wolfcode.crm.domain.Menu;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IMenuService;
import cn.wolfcode.crm.util.JSONResult;
import cn.wolfcode.crm.util.PageResult;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("menu")
public class MenuController {
    @Autowired
    public IMenuService menuService;

    @RequestMapping("view")
    @RequiresPermissions(value = {"menu:view","菜单列表"},logical = Logical.OR)
    public String view() {
        return "menu/menu";
    }

    @ResponseBody
    @RequestMapping("query")
    public PageResult query(QueryObject qo) {
        return menuService.query(qo);
    }

    @RequestMapping("selectAll")
    @ResponseBody
    public List<Menu> selectAll() {
        return menuService.selectAll();
    }

    @RequestMapping("delete")
    @RequiresPermissions(value = {"menu:delete","菜单删除"},logical = Logical.OR)
    @ResponseBody
    public JSONResult delete(Long id) {
        try {
            menuService.deleteByPrimaryKey(id);
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("删除失败");
        }
    }
    @ResponseBody
    @RequestMapping("saveOrUpdate")
    @RequiresPermissions(value = {"menu:saveOrUpdate","菜单新增或编辑"},logical = Logical.OR)
    public JSONResult saveOrUpdate(Menu dept){
        try {
            if(dept.getId() != null){
                menuService.updateByPrimaryKey(dept);
            }else{
                menuService.insert(dept);
            }
            return JSONResult.success("");
        } catch (Exception e) {
            e.printStackTrace();
            return JSONResult.mark("操作失败");
        }
    }
    @ResponseBody
    @RequestMapping("getRootMenu")
    public List<Menu> getRootMenu(){
        return menuService.getRootMenu();
    }
    @ResponseBody
    @RequestMapping("getEmployeeMenu")
    public List<Menu> getEmployeeMenu(){
        return menuService.getEmployeeMenu();
    }
}
